<template>
  <el-dialog v-model="isShowDialog"
             :before-close="handleClose"
             title="生成采购任务"
             draggable
             width="400px"
             append-to-body
  >

    <el-form :inline="true" ref="formRef" :model="saveParams" label-width="100px" :rules="rules">
      <el-form-item label="任务天数" prop="day">
        <el-input v-model="saveParams.day" @input="saveParams.day=limitIntNumber(saveParams.day)"  :maxlength="3"/>
      </el-form-item>
      <el-form-item label="任务数量" prop="totalNum">
        <el-input v-model="saveParams.totalNum" @input="saveParams.totalNum=limitIntNumber(saveParams.totalNum)" :maxlength="5"/>
      </el-form-item>
    </el-form>

    <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="clickSave"> 保存 </el-button>
          <el-button @click="handleClose">取消</el-button>
        </span>
    </template>

  </el-dialog>
</template>

<script setup>
import {getCurrentInstance, ref} from "vue";
import {saveGenerator} from "@/api/scApi";

const { proxy } = getCurrentInstance()
// 表单校验
const rules = ref({
  day: [{required: true, message: "任务天数不能为空", trigger: "blur"}],
  totalNum: [{required: true, message: "任务数量不能为空", trigger: "blur"}],
});

const props = defineProps({
  isShowDialog: {
    type: Boolean,
    default: false
  },
})
let saveParams=ref({})
const emits = defineEmits(['cancel'])
function handleClose() {
  emits('cancel')
}

function clickSave(){
  proxy.$refs.formRef.validate((valid, fileds) => {
    if (valid) {
      proxy.$modal.confirm('将生成的采购数据:近'+saveParams.value.day+"天,销售数量>="+saveParams.value.totalNum+"单,是否继续?").then(() => {
        saveGenerator(saveParams.value).then(res => {
          if (res.code === 200) {
            proxy.$modal.msgSuccess('保存成功')
            emits('cancel', true)
          }
        })
      }).catch(() => {
      });
    }
  })
}

</script>

<style scoped lang="scss">

</style>